package com.boyu.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boyu.common.BaseController;
import com.boyu.common.SystemConst;
import com.boyu.common.model.Address;
import com.boyu.common.model.Fuser;
import com.boyu.common.model.OrderProduct;
import com.boyu.common.model.Product;

/**
 * @author changan.hca
 * @date 2017/7/16
 */
public class OrderController extends BaseController {

    private Integer getUid() {
        Fuser fuser = this.getSessionAttr(SystemConst.FRONT_USER_SESSION);
        if (fuser == null) {
            return null;
        }
        return fuser.getId();
    }

    public void cart() {
        Integer pid = this.getParaToInt("id");
        if (pid == null) {
            this.render("list.html");
            return;
        }
        Integer uid = this.getUid();
        if (uid == null) {
            this.render("list.html");
            return;
        }
        List<OrderProduct> list = OrderProduct.dao.find("select * from t_order_product where uid=" + uid);
        OrderProduct orderProduct = null;
        for (OrderProduct op : list) {
            if (pid == op.getPid().intValue()) {
                orderProduct = op;
                break;
            }
        }
        if (orderProduct == null) {
            orderProduct = new OrderProduct();
            orderProduct.setPid(pid);
            orderProduct.setCreateTime(new Date());
            orderProduct.setUid(uid);
            orderProduct.setNum(1);
        } else {
            orderProduct.setNum(orderProduct.getNum() + 1);
        }
        orderProduct.save();
        this.cartList();
    }

    public void cartList() {
        Integer uid = this.getUid();
        if (uid == null) {
            this.render("list.html");
            return;
        }
        List<OrderProduct> list = OrderProduct.dao.find("select * from t_order_product where uid=" + uid);
        String pids = "";
        for (OrderProduct op : list) {
            pids = op.getPid() + ",";
        }
        if (pids.length() > 0) {
            pids = pids.substring(0, pids.length() - 1);
            List<Product> products = Product.dao.find("select * from t_product where id in(" + pids + ")");
            Map<Integer, Product> pmap = new HashMap<>();
            for (Product p : products) {
                pmap.put(p.getId(), p);
            }
            this.setAttr("pmap", pmap);
        }
        this.setAttr("list", list);
        this.render("list.html");
    }

    public void delCart() {
        Integer opid = this.getParaToInt("opid");
        OrderProduct.dao.deleteById(opid);
        this.cartList();
    }

    public void clearCart() {
        Integer uid = this.getUid();
        if (uid == null) {
            this.render("list.html");
            return;
        }
        List<OrderProduct> orderProducts = OrderProduct.dao.find("select * from t_order_product where uid=" + uid);
        List<Integer> list = new ArrayList<>();
        for (OrderProduct op : orderProducts) {
            list.add(op.getId());
        }
        OrderProduct.dao.deleteById(list);
        this.render("list.html");
    }

    public void payAddress() {
        Integer uid = this.getUid();
        if (uid == null) {
            this.render("list.html");
            return;
        }
        List<Address> addresses = Address.dao.find("select * from t_address where uid = " + uid);
        if (addresses != null && addresses.size() > 0) {
            this.setAttr("address", addresses.get(0));
        }
        this.render("address.html");
    }

    public void address() {
        Address address = this.getModel(Address.class);
        if (address.getId() > 0) {
            address.update();
        } else {
            address.save();
        }
        this.render("pay.html");
    }

    public void pay() {

        this.render("tip.html");
    }
}
